package com.yunkeji.api.pay.controller;

import cn.hutool.json.JSONObject;
import com.yunkeji.api.pay.service.PayService;
import com.yunkeji.common.core.controller.BaseController;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/** 支付 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/piggy/piggyCallBack")
public class PiggyCallBackController extends BaseController {
    private final PayService payService;
    /**
     * 小猪电签回调
     *
     * @author 老张
     * @return
     */
    @PostMapping(value = "/piggySignCallBack")
    @ResponseBody
    public String piggySignCallBack(@RequestBody JSONObject notify) {
        JSONObject data = notify.getJSONObject("data");
        if (data != null) {
            JSONObject customParams = data.getJSONObject("customParams");
            if (customParams != null) {
                if (customParams.getLong("uid") != null) {
                    payService.piggySignCallBack(notify);
                    return "success";
                }
            }
        }
        return "";
    }

    /**
     * 小猪放款回调
     *
     * @author 老张
     * @return
     */
    @PostMapping(value = "/piggyPayCallBack")
    @ResponseBody
    public String piggyPayCallBack(@RequestBody JSONObject notify) {
        log.info("小猪提现异步通知:{}", notify.toStringPretty());
        String isSuccess = notify.getStr("isSuccess");
        if ("T".equals(isSuccess)) {
            boolean result  = payService.piggyPayCallBack(notify.getJSONObject("data").getStr("bizAESContent"));
            return result ? "success" : "failure";
        } else {
            return "";
        }
    }
}
